using System;
using Jalal.Library.DataStructure;
using Jalal.Library.Data;
using BCGC.DAL;
using Jalal.Library.Utility;
using System.Data;
using BCGC.BLL.Helper;

namespace BCGC.BLL.Forum
{

    public class Post
    {

        private int postID;
        public int PostID
        {
            get
            {
                return postID;
            }
        }

        private int categoryID;
        public int CategoryID
        {
            get
            {
                return categoryID;
            }
            set
            {
                categoryID = value;
            }
        }

        private int userID;
        public int UserID
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        private String userName;
        public String UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
            }
        }

        private String userAvater;
        public String UserAvater
        {
            get
            {
                return userAvater;
            }
            set
            {
                userAvater = value;
            }
        }

        private String userMode;
        public String UserMode
        {
            get
            {
                return userMode;
            }
            set
            {
                userMode = value;
            }
        }

        private String postTitle;
        public String PostTitle
        {
            get
            {
                return postTitle;
            }
            set
            {
                postTitle = value;
            }
        }

        private DateTime postDate;
        public DateTime PostDate
        {
            get
            {
                return postDate;
            }
            set
            {
                postDate = value;
            }
        }

        private String postContent;
        public String PostContent
        {
            get
            {
                return postContent;
            }
            set
            {
                postContent = value;
            }
        }

        private int tag;
        public int Tag
        {
            get
            {
                return tag;
            }
            set
            {
                tag = value;
            }
        }

        private bool isReply;
        public bool IsReply
        {
            get
            {
                return isReply;
            }
            set
            {
                isReply = value;
            }
        }

        private int replyTOID;
        public int ReplyTOID
        {
            get
            {
                return replyTOID;
            }
            set
            {
                replyTOID = value;
            }
        }

        private int displayOrder;
        public int DisplayOrder
        {
            get
            {
                return displayOrder;
            }
            set
            {
                displayOrder = value;
            }
        }

        private bool isActive;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                isActive = value;
            }
        }

        private int latestPostID;

        public int LatestPostID
        {
            get { return latestPostID; }
            set { latestPostID = value; }
        }

        internal Post() { }

        internal Post(DataRow dataRow)
        {
            if (dataRow != null)
            {
                postID = (int)CommonUtility.FilterNull(dataRow["PostID"], typeof(int));
                categoryID = (int)CommonUtility.FilterNull(dataRow["CategoryID"], typeof(int));
                userID = (int)CommonUtility.FilterNull(dataRow["UserID"], typeof(int));
                userName = (string)CommonUtility.FilterNull(dataRow["UserName"], typeof(string)); 
                userMode = (string)CommonUtility.FilterNull(dataRow["Mood"], typeof(string));
                userAvater = SiteUtility.FormateImage(((string)CommonUtility.FilterNull(dataRow["AvatarImageUrl"], typeof(string))),ImageType.UserImage);
                postTitle = (string)CommonUtility.FilterNull(dataRow["PostTitle"], typeof(string));
                postContent = (string)CommonUtility.FilterNull(dataRow["PostContent"], typeof(string));
                postDate = (DateTime)CommonUtility.FilterNull(dataRow["PostDate"], typeof(DateTime));
                tag = (int)CommonUtility.FilterNull(dataRow["TagID"], typeof(int));
                isReply = (bool)CommonUtility.FilterNull(dataRow["IsReply"], typeof(bool));
                replyTOID = (int)CommonUtility.FilterNull(dataRow["ReplyToID"], typeof(int));
                displayOrder = (int)CommonUtility.FilterNull(dataRow["DisplayOrder"], typeof(int));
                isActive = (bool)CommonUtility.FilterNull(dataRow["IsActive"], typeof(bool));
                latestPostID = (int)CommonUtility.FilterNull(dataRow["LatestPostID"], typeof(int));
                
            }
         }

        private static ReadWriteCustomCollection<Post> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<Post> collection = new ReadWriteCustomCollection<Post>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new Post(dr));
            }
            return collection;
        }

        private int Create()
        {
            int result = PostData.CreatePost(this.CategoryID,this.UserID,this.PostTitle,this.PostContent,this.PostDate,this.Tag,this.IsReply,this.ReplyTOID,this.DisplayOrder,this.IsActive,this.LatestPostID);
            return result;
        }

        private bool Update()
        {
            bool result = PostData.UpdatePost(this.PostID,this.CategoryID, this.UserID, this.PostTitle, this.PostContent, this.PostDate, this.Tag, this.IsReply, this.ReplyTOID, this.DisplayOrder,this.IsActive,this.LatestPostID);
            return result;
        }
        internal bool UpdateLatestPost(int postID, int latestPostID)
        {
            bool result = PostData.UpdateLatestPost(postID, latestPostID);
            return result;
        }

        internal bool Save()
        {
            if (this.PostID != 0)
            {
                return Update();
            }
            else
            {
                int result = Create();
                if (result > 0)
                {
                    this.postID = result;
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        internal static bool Delete(int postID)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            bool result = PostData.DeletePost(postID);
            DataFactory.GetDataMiner().CommitTransaction();
            return result;

        }

        internal static ReadWriteCustomCollection<Post> GetAllByCategory(int categoryID)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostData.GetPostByCategory(categoryID);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }

        internal static Post GetByID(int postID)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostData.GetPostByPostID(postID);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new Post(result.Rows[0]);
            else
                return null;
        }

        internal static ReadWriteCustomCollection<Post> GetPostDetailsByID(int postID)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostData.GetPostDetailsByPostID(postID);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }


        internal static bool DeactivatePost(int postID)
        {
            bool result = PostData.DeactivatePost(postID);
            return result;
        }

        

        internal static ReadWriteCustomCollection<Post> GetAll(int? categoryId, int? userID, string tag, string sortColumn, string sortDirection, int currentPageIndex, int pageSize, bool showReply,out int totalRecords)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = PostData.GetAllPost(categoryId, userID, tag, sortColumn, sortDirection, currentPageIndex, pageSize,showReply,out totalRecords);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }
    }

}
